package com.qs.eggs.view.activity;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.SearchView;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qs.eggs.R;
import com.qs.eggs.adapter.SortTabIndicatorAdapter;
import com.qs.eggs.adapter.SortTabIndicatorAdapterNew;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.listener.SortOnTransitionListener;
import com.qs.eggs.view.fragment.JiaGeSortFragment;
import com.qs.eggs.view.fragment.XiaoLiangSortFragment;
import com.qs.eggs.view.fragment.ZongheSortFragment;
import com.shizhefei.view.indicator.FixedIndicatorView;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;

import java.util.ArrayList;

import static com.lzy.okgo.OkGo.getContext;

public class SortActivity extends BaseActivity {
    private FixedIndicatorView indicator;
    private IndicatorViewPager indicatorViewPager;
    private ImageView mBack;
    private SearchView mSearch;
    private String[] tabName = new String[] {"综合","销量", "价格"};
    private ArrayList<Fragment> fmList;
    private ViewPager viewPager;

    public static Activity instance;

    @Override
    protected int getContentView() {
        return R.layout.activity_sort;
    }

    @Override
    protected void initWidget() {
        super.initWidget();

        instance=this;
        mBack=(ImageView)findViewById(R.id.img_back);
        mSearch=(SearchView)findViewById(R.id.poi_et_search);
        indicator=(FixedIndicatorView)findViewById(R.id.guide_indicator);
        viewPager=(ViewPager)findViewById(R.id.guide_viewPager);
        if(getIntent().getStringExtra("biaoshi").equals("search")){
            mSearch.setQueryHint(getIntent().getStringExtra("pronameLIKE"));
        }
        fmList=new ArrayList<>();
        fmList.add(new ZongheSortFragment());
        fmList.add(new XiaoLiangSortFragment());
        fmList.add(new JiaGeSortFragment());
        // 将viewPager和indicator使用
        indicator.setItemClickable(false);
        if(mSearch==null){
            return;
        }else{
//获取到TextView的控件
            TextView textView = (TextView) mSearch.findViewById(android.support.v7.appcompat.R.id.search_src_text);
//设置字体大小为14sp
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);//14sp
        }
        indicatorViewPager = new IndicatorViewPager(indicator, viewPager,true);
        ViewPagerSetting();

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }


    private void ViewPagerSetting() {

        // 指示条
        indicator.setScrollBar(new ColorBar(getContext(), ContextCompat.getColor(getContext(),R.color.price), 8));
        // 未选中字体大小
        float unSelectSize = 20;
        // 选中字体大小
        float selectSize = unSelectSize * 1.0f;
        // 未选中的颜色
        int unSelectColor = Color.parseColor("#bbbbbb");
        // 选中时的颜色
        int selectColor = ContextCompat.getColor(getContext(),R.color.price);
        indicator.setItemClickable(true);
        // 设置tab字体的变化
        indicator.setOnTransitionListener(new OnTransitionTextListener(
                selectSize, unSelectSize, selectColor, unSelectColor));

        viewPager.setOffscreenPageLimit(2);
        indicatorViewPager = new IndicatorViewPager(indicator, viewPager);
        // 设置适配器
        indicatorViewPager.setAdapter(new SortTabIndicatorAdapterNew(getSupportFragmentManager(),this,fmList,tabName));
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {

                viewPager.setCurrentItem(currentItem);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSearch.clearFocus();
    }
}
